//
//  WPSoftwareAgreementVC.m
//  wobingwoyi
//
//  Created by WGP on 16/9/14.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPSoftwareAgreementVC.h"
#import <WebKit/WebKit.h>

@interface WPSoftwareAgreementVC ()<WKNavigationDelegate>

@property(nonatomic,strong)UIView *statusView;
@property(nonatomic,strong)UIView *navView;
@property(nonatomic,strong)WKWebView *webView;
@property (weak, nonatomic) CALayer *progresslayer;

@end
@implementation WPSoftwareAgreementVC

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    self.view.backgroundColor = GlobalBgColor;
    [self setupUI];
    
    //进度条
    UIView *progress = [[UIView alloc]initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.frame), 2)];
    progress.backgroundColor = [UIColor clearColor];
    [self.view addSubview:progress];
    
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(0, 0, 0, 1);
    layer.backgroundColor = GlobalColor.CGColor;
    [progress.layer addSublayer:layer];
    self.progresslayer = layer;
}

-(void)setupUI
{
    [self statusView];
    [self navView];
    [self webView];
}

-(UIView *)statusView
{
    if (_statusView == nil)
    {
        _statusView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
        _statusView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_statusView];
    }
    return _statusView;
}

-(UIView *)navView
{
    if (_navView == nil)
    {
        _navView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 44)];
        _navView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_navView];
        
        UILabel *label = [UILabel new];
        label.text = self.PageTitle;
        label.font = [UIFont systemFontOfSize:18];
        label.textColor = GLobalFontColor;
        [label sizeToFit];
        [_navView addSubview:label];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(_navView);
        }];
        
        UIImageView *imageView = [UIImageView new];
        imageView.backgroundColor = RGB_SAME(180);
        [_navView addSubview:imageView];
        
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0.5);
            make.left.right.bottom.equalTo(_navView);
        }];
        
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [backBtn setBackgroundImage:[UIImage imageNamed:@"back_normal"] forState:UIControlStateNormal];
        [backBtn setBackgroundImage:[UIImage imageNamed:@"back_highlight"] forState:UIControlStateHighlighted];
        [backBtn sizeToFit];
        [backBtn addTarget:self action:@selector(backBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
        [_navView addSubview:backBtn];
        
        [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_navView);
            make.left.equalTo(_navView).offset(10);
        }];
    }
    return _navView;
}

-(WKWebView *)webView
{
    if (_webView == nil)
    {
        _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
        _webView.navigationDelegate = self;
        [_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
        [self.view addSubview:_webView];
        
        [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
    }
    return _webView;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        self.progresslayer.opacity = 1;
        if ([change[@"new"] floatValue] < [change[@"old"] floatValue]) {
            return;
        }
        self.progresslayer.frame = CGRectMake(0, 0, self.view.bounds.size.width * [change[@"new"] floatValue], 2);
        if ([change[@"new"] floatValue] == 1) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                self.progresslayer.opacity = 0;
            });
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                self.progresslayer.frame = CGRectMake(0, 0, 0, 1);
            });
        }
    }else{
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation
{
//    WPLog(@"开始加载用户协议");
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation
{
    [self dismiss];
}

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error
{
    [self showErrorInfoWithStatus:@"加载失败"];
//    NSLog(@"%@",error);
}

-(void)backBtnDidClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)dealloc
{
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
}

@end
